package com.tang.single;

import lombok.SneakyThrows;

import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;

/**
 * @author Tang
 * @classname WorkHandler
 * @description [ 业务处理 ]
 * @date 2021/8/10 14:37
 */
public class WorkHandler implements Runnable{

    private SocketChannel socketChannel;

    private SelectionKey selectionKey;

    private ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

    public WorkHandler(SocketChannel socketChannel, SelectionKey selectionKey) {
        this.socketChannel = socketChannel;
        this.selectionKey = selectionKey;
    }

    @SneakyThrows
    @Override
    public void run() {
        if (this.selectionKey.isReadable()) {
            int len = this.socketChannel.read(byteBuffer);
            byteBuffer.flip();
            System.out.println("receive msg:--->" + new String(byteBuffer.array(),0,len));
            byteBuffer.clear();
        }
    }
}